﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;
using ObjectDetection;


/// <summary>
/// GuidingRobot命名空间实现导游机器人的各种功能
/// @author Jie Deng
/// @version 1.0
/// </summary>
namespace GuidingRobot
{
   
    /// <summary>
    /// 图像处理的类来完成导游机器人展板识别功能的类
    /// </summary>
    public class BitmapProcessITypeReco : ITypeReco
    {
        private Bitmap mybitmap;    //需要处理的图像


        /**
         * 无参构造函数
         */
        public BitmapProcessITypeReco() { }


        /**
         * 构造函数
         * @param Bitmap
         */
        public BitmapProcessITypeReco(Bitmap mybitmap)
        {
            this.mybitmap = mybitmap;
        }


        /**
         * 构造函数
         * @param string
         */
        public BitmapProcessITypeReco(string bitmappath)
        {
            this.mybitmap = (Bitmap)Image.FromFile(bitmappath);
        }


        /**
         * 导入Asprise OCR SDK 4.0
         */
        [DllImport("AspriseOCR.dll", EntryPoint = "OCR")]
        public static extern IntPtr OCR(string file, int type);
        /**
         * 获取图片经过OCR后的字符
         * @param string类型，图片的路径
         * @return string类型，识别后的字符串
         */
        private static string GetRecoString(string bitmappath)
        {
            return Marshal.PtrToStringAnsi(OCR(bitmappath, -1));
        }


        /**
         * 判断图像中存在展板标签的类型
         * @param string类型，图片的路径
         * @return int类型，不存在标签返回0，其他为1,2,3...
         */
        public int GetLabelType(string bitmappath)
        {
            string str = GetRecoString(bitmappath);
            int type = 0;
            for (int i = 0; i < 20; i++)
            {
                type = str.IndexOf(i.ToString());
                if (type == -1)
                {
                    type = 0;
                    continue;
                }
                else
                {
                    type = i;
                    break;
                }
            }
            return type;
        }


        /**
         * @param string类型，图片路径
         * @return bool类型，存在展板返回true，否则返回false
         */ 
        public bool HasLabel(string bitmappath)
        {
            if (GetLabelType(bitmappath)==0)
            {
                return false;
            } 
            else
            {
                return true;
            }
        }


    }


    /// <summary>
    /// 图像处理的类来完成导游机器人寻线功能的类，采用的是霍夫变换实现IRobotGoDAV接口
    /// </summary>
    public class BitmapProcessIRobotGoDAV : IRobotGoDAV
    {
        private Bitmap mybitmap;    //需要处理的图像

        private int angle;          //图像中直线偏转角度

        /**
         * 无参私有构造函数
         */
        private BitmapProcessIRobotGoDAV() { }


        /**
         * 构造函数
         * @param Bitmap
         */
        public BitmapProcessIRobotGoDAV(Bitmap mybitmap)
        {
            this.mybitmap = mybitmap;
            SetAngle();
        }


        /**
         * 构造函数
         * @param string
         */
        public BitmapProcessIRobotGoDAV(string bitmappath)
        {
            this.mybitmap = (Bitmap)Image.FromFile(bitmappath);
            SetAngle();
        }


        private void SetAngle()
        {
            LineDetection ld = new LineDetection(new Bitmap(mybitmap));
            int ang = ld.GetAngle();
            if (ang != -1)
            {
                this.angle = ang;
            }
            else
            {
                this.angle = -1;
            }

        }


        public int SetRobotGoDirection()
        {
            if (angle > 85 && angle < 95)
            {
                return 0;
            }
            else if (angle == -1)
            {
                return 0;
            }
            else if (angle >= 95)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }


        public int SetRobotGoAngle()
        {
            if (angle > 85 && angle < 95)
            {
                return 0;
            }
            else if (angle == -1)      //没检查到直线就朝直走
            {
                return 0;
            }
            else if (angle >= 95)
            {
                return angle - 90;
            }
            else
            {
                return 90 - angle;
            }
        }


        public int SetRobotGoVelocity(int v)
        {
            return v;
        }

    }


    /// <summary>
    /// 图像处理的类来完成导游机器人寻线功能的类，实现IRobotGoSV接口
    /// </summary>
    public class BitmapProcessIRobotSV : IRobotGoSV
    {
        private Bitmap mybitmap;    //需要处理的图像

        /**
         * 无参私有构造函数
         */
        private BitmapProcessIRobotSV() { }


        /**
         * 构造函数
         * @param Bitmap
         */
        public BitmapProcessIRobotSV(Bitmap mybitmap)
        {
            this.mybitmap = mybitmap;
        }


        /**
         * 构造函数
         * @param string
         */
        public BitmapProcessIRobotSV(string bitmappath)
        {
            this.mybitmap = (Bitmap)Image.FromFile(bitmappath);
        }


        public sbyte SetRobotGoScale()
        {
            LineDetection ld = new LineDetection(new Bitmap(mybitmap));
            return ld.GetScale();
        }


        public sbyte SetRobotVelocity(sbyte v)
        {
            return v;
        }
    }


}
